
const setZero = value=>{
	return value = (value < 10 ? '0': '')+value
}
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'undefined' || time === null || time === 'null') {
    return ''
  } else if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    if (result.length > 0 && value < 10) {
      value = setZero(value)
    }
    return value || 0
  })
  return time_str
}
//获取当前路径
export function getCurrentRoute() {
  const  pages = getCurrentPages();
  if (pages.length > 0) {
		const currentPage = pages[pages.length - 1];
		return currentPage.route;
  }
  return '/'
}
import dayjs from 'dayjs';
export function formatTime(time, option) {
	if(typeof(time)=='string'&&time.indexOf(':')===-1){
		time = Number(time)
	}
	time = dayjs(time).unix(); 
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000
	const nowD = dayjs().format('d');
	const preDay = 7 - nowD;
	const hm = dayjs(time).format('HH:mm')
  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600 * 24 && diff/3600-dayjs().format('HH')<0) {
    return hm
  } else if (diff < 3600 * 24 * 2 ){
		return '昨天 ' + hm
	}else if(diff < 3600 * 24 * preDay ){
		return '周'+parseTime(time,'{a}')+ hm
	}else if(dayjs(time).format('YYYY')===dayjs().format('YYYY')){
		return dayjs(time).format('MM月DD日')
	}
  if (option) {
    return dayjs(time).format(option)
  } else {
		return dayjs(time).format('YY年MM月DD日')
  }
}

//拆分地址
export function formatLocation(res) {
  const address = res[0].regeocodeData.addressComponent;
	return {
		REGION_PROVINCE: address.province,
		REGION_COUNTRY:  address.city,
		REGION_CITY:  address.district,
		ADDRESS:  res[0].regeocodeData.formatted_address
	}
};
//格式化省市区
export function cityPathFormat(val){
	if(!val) return '';
	return val.replace(/\,/g,'-')
}

// px 转rpx
export function pxToRpx(px){
	if(px<1){
		return px
	}
	const pixelRatio = 750 / uni.getSystemInfoSync().windowWidth;
	const rpx = px * pixelRatio;
	return rpx
}